home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-tk / samples.tk / cursor.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  4KB  |  205 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "gltk.h"
  29.  
  30. GLenum rgb, doubleBuffer, directRender, windType;
  31. int windX, windY;
  32. int cursor;
  33. GLubyte shape[] =
  34. {
  35.   0x00, 0x00,
  36.   0x7F, 0xFE,
  37.   0x40, 0x02,
  38.   0x40, 0x02,
  39.   0x40, 0x02,
  40.   0x40, 0x02,
  41.   0x40, 0x02,
  42.   0x40, 0x02,
  43.   0x40, 0x02,
  44.   0x40, 0x02,
  45.   0x40, 0x02,
  46.   0x40, 0x02,
  47.   0x40, 0x02,
  48.   0x40, 0x02,
  49.   0x7F, 0xFE,
  50.   0x00, 0x00
  51. };
  52. GLubyte mask[] =
  53. {
  54.   0xFF, 0xFF,
  55.   0xFF, 0xFF,
  56.   0xFF, 0xFF,
  57.   0xE0, 0x07,
  58.   0xE0, 0x07,
  59.   0xE0, 0x07,
  60.   0xE0, 0x07,
  61.   0xE0, 0x07,
  62.   0xE0, 0x07,
  63.   0xE0, 0x07,
  64.   0xE0, 0x07,
  65.   0xE0, 0x07,
  66.   0xE0, 0x07,
  67.   0xFF, 0xFF,
  68.   0xFF, 0xFF,
  69.   0xFF, 0xFF
  70. };
  71.  
  72. static void Init(void)
  73. {
  74.   int i;
  75.  
  76.   for (i = TK_BLACK; i <= TK_WHITE; i++) {
  77.     tkNewCursor(i, shape, mask, i, TK_WHITE, 0, 0);
  78.   }
  79.   for (i = TK_BLACK; i <= TK_WHITE; i++) {
  80.     tkNewCursor(i + TK_WHITE, shape, mask, TK_WHITE, i, 0, 0);
  81.   }
  82.   cursor = TK_BLACK;
  83.   tkSetCursor(cursor);
  84.   glClearColor(0.0, 0.0, 0.0, 0.0);
  85.   glClearIndex(0.0);
  86. }
  87.  
  88. static void Reshape(int width, int height)
  89. {
  90.  
  91.   windX = width;
  92.   windY = height;
  93.   glViewport(0, 0, windX, windY);
  94.  
  95.   glMatrixMode(GL_PROJECTION);
  96.   glLoadIdentity();
  97.   gluOrtho2D(0, windX, 0, windY);
  98.   glMatrixMode(GL_MODELVIEW);
  99. }
  100.  
  101. static GLenum Key(int key, GLenum mask)
  102. {
  103.  
  104.   switch (key) {
  105.     case TK_ESCAPE:
  106.       tkQuit();
  107.     case TK_SPACE:
  108.       cursor++;
  109.       if (cursor > TK_WHITE * 2) {
  110.     cursor = TK_BLACK;
  111.       }
  112.       tkSetCursor(cursor);
  113.     default:
  114.       return GL_FALSE;
  115.   }
  116.   return GL_TRUE;
  117. }
  118.  
  119. static void Draw(void)
  120. {
  121.  
  122.   glClear(GL_COLOR_BUFFER_BIT);
  123.  
  124.   glBegin(GL_POLYGON);
  125.   TK_SETCOLOR(windType, TK_BLACK);
  126.   glVertex2i(0, 0);
  127.   TK_SETCOLOR(windType, TK_RED);
  128.   glVertex2i(windX, 0);
  129.   TK_SETCOLOR(windType, TK_GREEN);
  130.   glVertex2i(windX, windY);
  131.   TK_SETCOLOR(windType, TK_BLUE);
  132.   glVertex2i(0, windY);
  133.   glEnd();
  134.  
  135.   glFlush();
  136.  
  137.   if (doubleBuffer) {
  138.     tkSwapBuffers();
  139.   }
  140. }
  141.  
  142. static GLenum Args(int argc, char **argv)
  143. {
  144.   GLint i;
  145.  
  146.   rgb = GL_TRUE;
  147.   doubleBuffer = GL_FALSE;
  148.   directRender = GL_TRUE;
  149.  
  150.   for (i = 1; i < argc; i++) {
  151.     if (strcmp(argv[i], "-ci") == 0) {
  152.       rgb = GL_FALSE;
  153.     }
  154.     else if (strcmp(argv[i], "-rgb") == 0) {
  155.       rgb = GL_TRUE;
  156.     }
  157.     else if (strcmp(argv[i], "-sb") == 0) {
  158.       doubleBuffer = GL_FALSE;
  159.     }
  160.     else if (strcmp(argv[i], "-db") == 0) {
  161.       doubleBuffer = GL_TRUE;
  162.     }
  163.     else if (strcmp(argv[i], "-dr") == 0) {
  164.       directRender = GL_TRUE;
  165.     }
  166.     else if (strcmp(argv[i], "-ir") == 0) {
  167.       directRender = GL_FALSE;
  168.     }
  169.     else {
  170.       printf("%s (Bad option).\n", argv[i]);
  171.       return GL_FALSE;
  172.     }
  173.   }
  174.   return GL_TRUE;
  175. }
  176.  
  177. void main(int argc, char **argv)
  178. {
  179.  
  180.   if (Args(argc, argv) == GL_FALSE) {
  181.     tkQuit();
  182.   }
  183.  
  184.   windX = 300;
  185.   windY = 300;
  186.   tkInitPosition(0, 0, windX, windY);
  187.  
  188.   windType = (rgb) ? TK_RGB : TK_INDEX;
  189.   windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  190.   windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  191.   tkInitDisplayMode(windType);
  192.  
  193.   if (tkInitWindow("Cursor Test") == GL_FALSE) {
  194.     tkQuit();
  195.   }
  196.  
  197.   Init();
  198.  
  199.   tkExposeFunc(Reshape);
  200.   tkReshapeFunc(Reshape);
  201.   tkKeyDownFunc(Key);
  202.   tkDisplayFunc(Draw);
  203.   tkExec();
  204. }
  205.